package com.santoker.user.service.service.common.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.santoker.user.service.dao.UserMapper;
import com.santoker.user.service.model.User;
import com.santoker.user.service.service.common.IUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author jiangs
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Override
    public User getUserByAccount(String account) {
        LambdaQueryWrapper<User> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(User::getIsDelete, 0)
                    .eq(User::getAccount, account);
        return getOne(queryWrapper, false);
    }

    @Override
    public User getUserByMobile(String mobile) {
        LambdaQueryWrapper<User> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(User::getIsDelete, 0)
                .eq(User::getMobile, mobile);
        return getOne(queryWrapper, false);
    }

    @Override
    public User getUserByEmail(String email) {
        LambdaQueryWrapper<User> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(User::getIsDelete, 0)
                .eq(User::getEmail, email);
        return getOne(queryWrapper, false);
    }

    @Override
    public User getUserByNickName(String nickName) {
        LambdaQueryWrapper<User> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(User::getIsDelete, 0)
                .eq(User::getNickname, nickName);
        return getOne(queryWrapper, false);
    }

    @Override
    public User getUserById(Integer userId) {
        LambdaQueryWrapper<User> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(User::getIsDelete, 0)
                .eq(User::getId, userId);
        return getOne(queryWrapper, false);
    }

    @Override
    public String getNickNameByUserId(Integer userId) {
        User user = baseMapper.selectById(userId);
        return Objects.isNull(user) ? "" : StringUtils.defaultString(user.getNickname());
    }

    @Override
    public List<User> findAll() {
        LambdaQueryWrapper<User> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(User::getIsDelete, 0)
                .orderByAsc(User::getCreatedAt);
        return list(queryWrapper);
    }

    @Override
    public List<User> findByNickNameLike(String userName) {
        LambdaQueryWrapper<User> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(User::getIsDelete, 0)
                .like(StringUtils.isNotBlank(userName), User::getNickname, userName)
                .orderByAsc(User::getCreatedAt);
        return list(queryWrapper);
    }

    @Override
    public List<User> findByMobileLike(String mobile) {
        LambdaQueryWrapper<User> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(User::getIsDelete, 0)
                .like(StringUtils.isNotBlank(mobile), User::getMobile, mobile)
                .orderByAsc(User::getCreatedAt);
        return list(queryWrapper);
    }

    @Override
    public User getUserByOpenid(String openid) {
        LambdaQueryWrapper<User> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(User::getIsDelete, 0)
                .eq(User::getOpenid, openid);
        return getOne(queryWrapper, false);
    }

}
